package com.sunp.email.springbootemail.service;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * @ClassName MailService
 * @Description 邮箱发送服务
 * @Author sunp
 * @Date 2018/11/29 14:29
 * @Version 0.0.1
 **/
@Service
public class MailService {

    private   final Logger logger = LoggerFactory.getLogger(MailService.class);

    @Value("${spring.mail.username}")
    private  String from ;

    @Autowired
    private JavaMailSender mailSender;


    /**
     * 发送邮件模板 文本文件
     * @param to 发送给谁
     * @param subject 主题是什么
     * @param content 邮件内容
     */
    public void sendSimpleEmail(String to,String subject,String content){
        logger.info("开始发送发送邮件模板 文本文件：to={},subject={},content={}",to,subject,content);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        simpleMailMessage.setFrom(from);
        mailSender.send(simpleMailMessage);
    }


    /**
     * 发送网页邮件
     * @param to
     * @param subject
     * @param content
     */
    public  void  sendHtmlEmail(String to,String subject,String content){
        logger.info("开始发送发送网页邮件：to={},subject={},content={}",to,subject,content);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            helper.setFrom(from);
            mailSender.send(message);
            logger.info("开始发送发送网页邮件成功 " );
        } catch (MessagingException e) {
            logger.error("开始发送发送网页邮件异常:"+e.getMessage());
        }

    }

    /**
     * 发送单个附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePath
     */
    public void  sendAttachmentMail(String to,String subject,String content,String filePath)  {
        logger.info("开始发送单个附件的邮件：to={},subject={},content={},filePath={}",to,subject,content,filePath);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            helper.setFrom(from);

            //读取文件
            FileSystemResource file  = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            helper.addAttachment(fileName,file);
            mailSender.send(message);
            logger.info("单个附件的邮件发送成功");
        } catch (MessagingException e) {
            logger.error("单个附件的邮件发送异常："+e.getMessage());
        }

    }

    /**
     * 发送多个附件邮件
     * @param to
     * @param subject
     * @param content
     * @param filePaths 文件地址集合
     */
    public void sendAttachmentsMail(String to,String subject,String content, List<String> filePaths)  {
        logger.info("发送多个附件邮件: to={},subject={},content={}，filePath={}",to,subject,content, JSON.toJSON(filePaths));
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            helper.setFrom(from);

            if (filePaths.size()>0){
                for (String filePath: filePaths){
                    //读取文件
                    FileSystemResource file  = new FileSystemResource(new File(filePath));
                    String fileName = file.getFilename();
                    helper.addAttachment(fileName,file);
                }
            }
            mailSender.send(message);
            logger.info("发送多个附件邮件成功");
        } catch (MessagingException e) {
            logger.info("发送多个附件邮件异常"+e.getMessage());
        }

    }


    /**
     * 发送图片的邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     * @throws MessagingException
     */
    public void sendInlineResourceMail(String to,String subject,String content,String rscPath,String rscId)  {

        logger.info("发送静态邮件开始：to={},subject={},content={},rscPath={},rscId={}",to,subject,content,rscPath,rscId);

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content,true);
            FileSystemResource rsc  = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId,rsc);
            logger.info("发送静态图片邮件成功");
        } catch (MessagingException e) {
            logger.error("发送静态邮件异常："+e.getMessage());
        }

    }














}
